var Util = new function () {
    this.getCardSeruakNumber = function () {
        var card = "";
        if (typeof CITV != "undefined") {
            card = CITV.loginInfo.smcId;
        } else if (typeof CAManager != "undefined") {
            card = CAManager.cardSerialNumber;
        } else {
            card = "000314463316";//"8280202374116016";
            //showMessage("用户信息异常，请退出重试！如有疑问，请致电96655！");
            console.log("获取到的是写死的卡号(方便测试用):" + card);
        }
        return card;
    };
    this.showLoading = function (gif_path, _msg) {
        var warpLoading = document.getElementById('zt-warp-loading');
        if (warpLoading == null) {
            warpLoading = document.createElement('div');
            warpLoading.setAttribute('id', 'zt-warp-loading');
            warpLoading.style.position = 'absolute';
            warpLoading.style.width = '1280px';
            warpLoading.style.height = '720px';
            document.body.appendChild(warpLoading);
        }

        var loadingImg = document.createElement('img');
        loadingImg.setAttribute('id', 'zt-img-loading');
        loadingImg.src = gif_path || 'img/loading.gif' || "../img/loading.gif";
        loadingImg.style.bottom = '0';
        loadingImg.style.position = 'absolute';
        loadingImg.style.top = '0';
        loadingImg.style.bottom = '0';
        loadingImg.style.left = '0';
        loadingImg.style.right = '0';
        loadingImg.style.margin = 'auto';
        loadingImg.style.zIndex = '999999';
        warpLoading.appendChild(loadingImg);
    };

    this.hideLoading = function () {
        var warp = document.getElementById('zt-warp-loading');
        if (warp != null) {
            document.body.removeChild(warp);
        }
    };
};

var errorMsg = {
    network: '(代码4201)网络通信异常，请退出后重新尝试。若有疑问，请联系当地客服热线。',
    parameter: '(代码4202)参数错误，请退出后重新尝试。若有疑问，请联系当地客户热线。',
    orderInfo: '(代码4203)获取订购信息失败，请退出后重新尝试。若有疑问，请联系当地客户热线。',
    navsError: '(代码4204)获取栏目异常，请退出后重新尝试。若有疑问，请联系当地客户热线。',
    noNavs: '暂无栏目信息！若有疑问，请联系当地客户热线。',
    contentError: '(代码4205)内容信息异常，请退出后重新尝试。若有疑问，请联系当地客户热线。',
    noContent: '暂无内容信息，请退出后重新尝试。若有疑问，请联系当地客户热线。',
    orderError: '(代码4206)退订信息异常，请退出后重新尝试。若有疑问，请联系当地客户热线。',
    nullUser: '(代码4207)用户信息为空，请退出后重新尝试。若有疑问，请联系当地客户热线。',
    userError: '(代码4205)用户信息异常，请退出后重新尝试。若有疑问，请联系当地客户热线。',
};


//删除样式
function removeClass(removeClassName, selectedDIV) {
    var classNames = selectedDIV.className;
    classNames = classNames.replace(/(^\s*)|(\s*$)/g, "");
    var classNameArr = classNames.split(" ");
    var okClassNames = "";
    for (var i = 0; i < classNameArr.length; i++) {
        if (!(classNameArr[i] == removeClassName)) {
            okClassNames += " " + classNameArr[i];
        }
    }
    selectedDIV.className = okClassNames;
}

function showMessage(mytext, img) {
    var imgSrc = "url('img/tip.png')";
    if (img != null) {
        imgSrc = "url('" + img + "')";
    }
    var selfAlert = document.getElementById("self_show");
    if (selfAlert != null) {
        selfAlert.style.display = "block";
        selfAlert.innerHTML = mytext;
        setTimeout("closeMessage()", 2000);
        return;
    }
    //背景div
    var bgObj = document.createElement("div");
    //获取当前窗口尺寸
    var sWidth = window.innerWidth ||
        document.documentElement.clientWidth ||
        document.body.clientWidth;

    var sHeight = window.innerHeight ||
        document.documentElement.clientHeight ||
        document.body.clientHeight;
    var msgw = 1000; //提示窗口的宽度
    var msgh = 60; //提示窗口的高度
    bgObj.setAttribute('id', 'self_show');
    bgObj.style.position = "absolute";
    bgObj.style.zIndex = "9999999";
    bgObj.style.width = msgw + "px";
    bgObj.style.height = msgh + "px";
    bgObj.style.fontSize = "24px";
    bgObj.style.background = imgSrc;
    bgObj.style.color = "white";
    bgObj.style.lineHeight = "60px";
    bgObj.style.textAlign = "center";
    bgObj.style.left = (sWidth - msgw) / 2 + "px";
    bgObj.style.top = (sHeight - msgh) / 2 + "px";
    document.body.appendChild(bgObj);
    var selfAlert = document.getElementById("self_show");
    selfAlert.innerHTML = mytext;
    setTimeout("closeMessage()", 3000)
}


function closeMessage() {
    var selfAlert = document.getElementById("self_show");
    if (selfAlert != null) {
        selfAlert.style.display = "none";
    }
}

function createXHR() {
    var xhr = null;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            xhr = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
            try {
                xhr = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e) {

            }
        }
    }
    return xhr;
}


// 遥控器按键逻辑算法
function getNearPos(n_div, all_div, aspect) {
    var result = null;
    var n_temp_div = n_div;
    var n_pos = new Array(parseInt(n_div.offsetLeft), parseInt(n_div.offsetTop), parseInt(n_div.offsetWidth), parseInt(n_div.offsetHeight));
    while (true) {
        if (n_temp_div.parentElement.id != "") {
            n_temp_div = n_temp_div.parentElement;
            n_pos[0] += parseInt(n_temp_div.offsetLeft);
            n_pos[1] += parseInt(n_temp_div.offsetTop);
        } else {
            break;
        }
    }
    var temp_pos = new Object();
    var temp_array = new Array();
    var temp_p_div = null;
    var temp_div_left = 0;
    var temp_div_top = 0;

    for (var i = 0; i < all_div.length;) {

        if (all_div[i] != n_div) {

            if (temp_p_div == null) {
                temp_p_div = all_div[i];
            }
            if (temp_p_div.parentElement.id != "") {

                temp_div_left = temp_div_left + parseInt(temp_p_div.parentElement.offsetLeft);
                temp_div_top = temp_div_top + parseInt(temp_p_div.parentElement.offsetTop);
                temp_p_div = temp_p_div.parentElement;
                continue;
            }

            temp_pos = new Object();
            temp_pos.left = temp_div_left + parseInt(all_div[i].offsetLeft);
            temp_pos.top = temp_div_top + parseInt(all_div[i].offsetTop);
            temp_pos.width = parseInt(all_div[i].offsetWidth);
            temp_pos.height = parseInt(all_div[i].offsetHeight);
            temp_pos.id = all_div[i].id;
            temp_array.push(temp_pos);
        }

        i++;
        temp_p_div = null;
        temp_div_left = 0;
        temp_div_top = 0;
    }

    var temp_x_con = -1;
    var juli_x = 0;
    var juli_y = -1;
    var temp_juli_y = -1;
    var temp_juli_x = -1;
    var temp_y = new Array();
    var temp_x = -1;

    switch (aspect) {
        case "up": {
            for (var i = 0; i < temp_array.length; i++) {
                if (n_pos[1] >= (temp_array[i].top + temp_array[i].height)) {
                    if (temp_juli_y < 0) {
                        temp_juli_y = n_pos[1] - (temp_array[i].top + temp_array[i].height);
                        temp_x_con = i;
                    } else {
                        if (n_pos[1] - (temp_array[i].top + temp_array[i].height) < temp_juli_y) {
                            temp_juli_y = n_pos[1] - (temp_array[i].top + temp_array[i].height);
                            temp_x_con = i;
                        }
                    }
                }
            }
            temp_y = new Array();
            if (temp_x_con >= 0) {
                for (var a = 0; a < temp_array.length; a++) {
                    if (temp_array[a].top + temp_array[a].height == temp_array[temp_x_con].top + temp_array[temp_x_con].height) {
                        temp_y.push(temp_array[a]);
                    }
                }
                var n_pos_m = n_pos[0] + n_pos[2] / 2;
                var temp_y_m = -1;
                for (var i = 0; i < temp_y.length; i++) {
                    temp_y_m = temp_y[i].left + temp_y[i].width / 2;
                    if (juli_y < 0 || (Math.abs(n_pos_m - temp_y_m) < juli_y)) {
                        juli_y = Math.abs(n_pos_m - temp_y_m);
                        temp_x = i;
                    } else {
                        if (Math.abs(n_pos_m - temp_y_m) == juli_y) {
                            if (temp_y[i].left < temp_y[temp_x].left) {
                                temp_x = i;
                            }
                        }
                    }
                }
                result = temp_y[temp_x];
            }
            break;
        }
        case "down": {

            for (var i = 0; i < temp_array.length; i++) {

                if (n_pos[1] + n_pos[3] <= temp_array[i].top) {
                    if (temp_juli_y < 0) {
                        temp_juli_y = temp_array[i].top - n_pos[1] + n_pos[3];
                        temp_x_con = i;
                    } else {
                        if (temp_array[i].top - n_pos[1] + n_pos[3] < temp_juli_y) {
                            temp_juli_y = temp_array[i].top - n_pos[1] + n_pos[3];
                            temp_x_con = i;
                        }
                    }
                }
            }

            temp_y = new Array();

            if (temp_x_con >= 0) {
                for (var a = 0; a < temp_array.length; a++) {
                    if (temp_array[a].top == temp_array[temp_x_con].top) {
                        temp_y.push(temp_array[a]);
                    }
                }
                var n_pos_m = n_pos[0] + n_pos[2] / 2;
                var temp_y_m = -1;
                //					console.log(" temp_y.length="+ temp_y.length);

                for (var i = 0; i < temp_y.length; i++) {
                    temp_y_m = temp_y[i].left + temp_y[i].width / 2;
                    if (juli_y < 0 || (Math.abs(n_pos_m - temp_y_m) < juli_y)) {
                        juli_y = Math.abs(n_pos_m - temp_y_m);
                        temp_x = i;
                    } else {
                        if (Math.abs(n_pos_m - temp_y_m) == juli_y) {
                            if (temp_y[i].left < temp_y[temp_x].left) {
                                temp_x = i;
                            }
                        }
                    }
                }
                result = temp_y[temp_x];
            }
            break;
        }
        case "left": {
            for (var i = 0; i < temp_array.length; i++) {
                if (n_pos[0] >= temp_array[i].left + temp_array[i].width) {
                    if (temp_juli_y < 0 || (n_pos[0] - temp_array[i].left + temp_array[i].width < temp_juli_y)) {
                        temp_juli_y = n_pos[0] - temp_array[i].left + temp_array[i].width;
                        temp_x_con = i;
                    }
                }
            }
            temp_y = new Array();
            if (temp_x_con >= 0) {
                for (var a = 0; a < temp_array.length; a++) {
                    //alert(temp_y[a].id);
                    if (temp_array[a].left + temp_array[a].width == temp_array[temp_x_con].left + temp_array[temp_x_con].width) {
                        temp_y.push(temp_array[a]);
                    }
                }
                var n_pos_m = n_pos[1] + n_pos[3] / 2;
                var temp_y_m = -1;
                for (var i = 0; i < temp_y.length; i++) {
                    temp_y_m = temp_y[i].top + temp_y[i].height / 2;
                    if (juli_y < 0 || (Math.abs(n_pos_m - temp_y_m) < juli_y)) {

                        juli_y = Math.abs(n_pos_m - temp_y_m);
                        temp_x = i;
                    } else {
                        if (Math.abs(n_pos_m - temp_y_m) == juli_y) {
                            if (temp_y[i].top < temp_y[temp_x].top) {
                                temp_x = i;
                            }
                        }
                    }
                }
                result = temp_y[temp_x];
            }
            break;
        }
        case "right": {
            for (var i = 0; i < temp_array.length; i++) {
                if (n_pos[0] + n_pos[2] <= temp_array[i].left) {
                    if (temp_juli_y < 0 || (temp_array[i].left - n_pos[0] + n_pos[2] < temp_juli_y)) {
                        temp_juli_y = temp_array[i].left - n_pos[0] + n_pos[2];
                        temp_x_con = i;
                    }
                }
            }
            temp_y = new Array();

            if (temp_x_con >= 0) {
                for (var a = 0; a < temp_array.length; a++) {

                    if (temp_array[a].left == temp_array[temp_x_con].left) {
                        temp_y.push(temp_array[a]);
                    }
                }
                //	alert(temp_y.length);
                var n_pos_m = n_pos[1] + n_pos[3] / 2;
                var temp_y_m = -1;
                for (var i = 0; i < temp_y.length; i++) {

                    temp_y_m = temp_y[i].top + temp_y[i].height / 2;
                    if (juli_y < 0 || (Math.abs(n_pos_m - temp_y_m) < juli_y)) {

                        juli_y = Math.abs(n_pos_m - temp_y_m);
                        temp_x = i;
                    } else {

                        if (Math.abs(n_pos_m - temp_y_m) == juli_y) {

                            if (temp_y[i].top < temp_y[temp_x].top) {
                                temp_x = i;
                            }
                        }
                    }
                }
                result = temp_y[temp_x];
            }
            break;
        }
    }
    if (result != null) {
        result = document.getElementById(result.id);
    }
    return result;
}

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
}


function $(id) {
    return document.getElementById(id);
}

//选择支付方式页面
function choosePay() {
    var time = getDate();
    var end = returnEndTime(time,insuranceObj.qixian.danwei,insuranceObj.qixian.number);
    focus = 3;
    var parNode = $("pay-info");
    if(insuranceObj.template.value=='accident'){
        parNode.innerHTML='<div class="pay-div"> <div class="pay-name">被保人年龄</div> <span>：0~80（岁）</span> </div>' +
            '<div class="pay-div"> <div class="pay-name">旅行周期</div> <span>：'+insuranceObj.qixian.number+insuranceObj.qixian.danwei+'</span> </div> ' +
            '<div class="pay-div"> <div class="pay-name">起保止期</div> <span>：'+end+'</span> </div> ' +
            '<div class="pay-div"> <div class="pay-name">投保人姓名</div> <span>：'+userObj.custname+'</span> </div> ' +
            '<div class="pay-div"> <div class="pay-name">投保人身份证</div> <span>：'+userObj.idcardid+'</span> </div>'+
            '<div class="pay-div"> <div class="pay-name">投保人手机号</div> <span>：'+userObj.phone+'</span> </div>'+
            '<div class="pay-div"> <div class="pay-name">与投保人关系</div> <span>：本人</span> </div>'+
            '<div class="pay-div"> <div class="pay-name">赔付标准</div> <span class="pay-information">：</span> </div>';
    }else{
        parNode.innerHTML='<div class="pay-div"> <div class="pay-name">房屋信息</div> <span>：'+userObj.addr+'</span> </div>' +
            '<div class="pay-div"> <div class="pay-name">起保日期</div> <span>：'+time+'</span> </div> ' +
            '<div class="pay-div"> <div class="pay-name">起保止期</div> <span>：'+end+'</span> </div> ' +
            '<div class="pay-div"> <div class="pay-name">投保人姓名</div> <span>：'+userObj.custname+'</span> </div> ' +
            '<div class="pay-div"> <div class="pay-name">与投保人关系</div> <span>：本人</span> </div>'+
            '<div class="pay-div"> <div class="pay-name">赔付标准</div> <span class="pay-information" id="zr">：</span> </div>';
    }
    showZR();
    $("pay-code").style.display = 'block';
    addBorderPay(pay);
}

function showZR() {
    var zr = $('zr');
    zr.innerHTML='';
    for(var i =0 ;i<insuranceObj.zeren.length;i++){
        zr.innerHTML+='<div>'+insuranceObj.zeren[i].name+'<br></div>'+
                '<div>'+insuranceObj.zeren[i].content+'<br></div>'+
                '<div>保额：'+insuranceObj.zeren[i].money+'</div>';
    }
}


//扫码页面
function code(type,money,fenshu) {
    var img = "img/b_05.png";
    var name = "支付宝";
    if(type==2){
        img = 'img/b_03.png';
        name = "微信";
    }
    focus = 4;
    var parNode = $("pay");
    parNode.innerHTML='';
    parNode.innerHTML += '<div class="code-title">请使用'+name+'扫码充值</div>' +
        '<div class="code-show" id="smCode"><img src="img/loading1.gif" class="smLoad"></div> ' +
        '<img class="code-type" src="'+img+'" id="">';
    parNode.style.display='block';
    //获取二维码
    var start = getDate();
    var end = returnEndTime(start,insuranceObj.qixian.danwei,insuranceObj.qixian.number);
    var info = '成都数博视科技有限公司-测试商品1';//'';
    //用户姓名 手机号 身份证号 费用 产品编号 开始时间  结束时间
    payment(userObj.custname,userObj.phone,userObj.idcardid,money,insuranceObj.id,start,end,info,type,fenshu);
}

//扫码成功页面
function successPage() {
    focus = 5;
    changeBalance = true;
    var parNode = $("pay");
    parNode.innerHTML='';
    parNode.innerHTML = '<img src="img/b_011.png" class="suImg"/>' +
        '<div class="suTip">购买成功！</div>' +
        '<div class="suResult suBack" id="suBack">返回首页</div>'+
        '<div class="suResult suOrder" id="suOrder">订单详情</div>'+
        '<div class="su-1" id="suActive"></div>';
    result = document.getElementById("suBack");
    parNode.style.display='block';
}
function errorPage() {
    focus = 5;
    changeBalance = false;
    var parNode = $("pay");
    parNode.innerHTML='';
    parNode.innerHTML = '<img src="img/a_03.png" class="erImg"/>' +
        '<div class="erTip">购买成功，错误代码E0053详情致电96655</div>' +
        '<div class="erBack" id="suBack">返回</div>';
    result = document.getElementById("suBack");
}

function getTimeC() {
    var date = new Date();
    var seperator = ":";
    var hours = date.getHours();
    var minutes = date.getMinutes();
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
    }
    var time = hours + seperator + minutes;
    return time;
}

//获取当前日期
function getDate() {
    var date = new Date();
    var seperator = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year+seperator+month + seperator + strDate;
    return currentdate;
}



function addDayGetTime(date, days) {
    var d = new Date(date.replace(/-/g,'/'));
    d.setDate(d.getDate() + parseInt(days));
    var m = d.getMonth() + 1;
    return d.getFullYear() + '-' + m + '-' + d.getDate();
}

function addMonthGetTime(date, months) {
    var d = new Date(date.replace(/-/g,'/'));
    d.setMonth(d.getMonth() + parseInt(months));
    var m = d.getMonth() + 1;
    return d.getFullYear() + '-' + m + '-' + d.getDate();
}

function addYearsGetTime(date, years) {
    var d = new Date(date.replace(/-/g,'/'));
    d.setFullYear(d.getFullYear() + parseInt(years));
    var m = d.getMonth() + 1;
    if(m<10){
        m = "0"+m;
    }
    var dd = d.getDate();
    if(dd<10){
        dd = "0"+dd
    }
    return d.getFullYear() + '-' + m + '-' + dd;
}

